﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.LIB;

namespace YArchitech.HVAC.PipeSetting
{
	public partial class FrmAddSysType : System.Windows.Forms.Form
	{
		public string NewName { get; set; }

		public FrmAddSysType()
		{
			this.InitializeComponent();
		}

		public FrmAddSysType(SystemTypeInfo systemType, IList<string> existNames) : this()
		{
			if (systemType == null || existNames == null)
			{
				throw new Exception("新建系统参数有误！");
			}
			this.systemType = systemType;
			this.existNames = existNames;
		}

		private void FrmAddSysType_Load(object sender, EventArgs e)
		{
			base.Icon = Resource.YArchitechIcon;
			string arg = this.systemType.IsParent ? "系统组" : "系统";
			this.Text = string.Format("请输入{0}名称", arg);
			this.tbName.Text = this.systemType.SysType + 1;
			this.tbName.Select();
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			string text = this.tbName.Text.Trim();
			if (string.IsNullOrEmpty(text))
			{
				YJKMessageBox.Show("系统名称不能为空，请重新输入！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				this.tbName.Select();
				return;
			}
			if (this.existNames.Contains(text))
			{
				YJKMessageBox.Show("已存在相同系统名称！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				this.tbName.Select();
				return;
			}
			this.NewName = text;
			base.DialogResult = DialogResult.OK;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void tbName_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\r')
			{
				this.btnOK_Click(sender, e);
			}
		}

		private SystemTypeInfo systemType;

		private IList<string> existNames;
	}
}
